package network;

import sun.rmi.runtime.NewThreadAction;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.PriorityQueue;
import java.util.Scanner;

public class TcpEchoClient {


    private Socket  socket = null;

    public TcpEchoClient(String ip, int port) throws IOException {
        socket = new Socket(ip, port);
    }

    public void start() {
        System.out.println("客服端启动");
        try (InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            PrintWriter writer = new PrintWriter(outputStream);
            Scanner scannerNetwork = new Scanner(inputStream);
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.print("请输入 -> : ");
                String request = scanner.next();
                writer.println(request);
                writer.flush();
                String response = scannerNetwork.next();
                System.out.println(response);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client = new TcpEchoClient("127.0.0.1", 9090);
        client.start();
    }
}

